<?php
/**
 * Author: Adam
 * Create at 2019/4/12.
 */

namespace App\Http\Controllers\Api\RSA;

use App\Http\Controllers\Controller;
use function GuzzleHttp\Psr7\str;

class RSAController extends Controller
{

    protected $sign_data = '123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211123213321321112321332132111232133213211';

    /**
     * 读取私钥
     * @return bool|resource
     * @author Adam
     * @date 2019/4/12 16:08
     */
    protected function getPrivateKey()
    {
        $private_key_content = file_get_contents(dirname(__FILE__) . '/rsa_key/private.key');
        return openssl_get_privatekey($private_key_content);
    }

    /**
     * 读取私钥
     * @return resource
     * @author Adam
     * @date 2019/4/12 16:09
     */
    protected function getPublicKey()
    {
        $public_key_content = file_get_contents(dirname(__FILE__) . '/rsa_key/public.key');
        return openssl_get_publickey($public_key_content);
    }

    /**
     * rsa数据签名
     * @return string
     * @author Adam
     * @date 2019/4/12 16:03
     */
    public function sign()
    {
        $private_key = $this->getPrivateKey();
        openssl_sign($this->sign_data, $sign, $private_key);
        openssl_free_key($private_key);
        return base64_encode($sign);
    }

    /**
     * rsa 数据验签
     * @return string
     * @author Adam
     * @date 2019/4/12 16:04
     */
    public function checkSign()
    {
        $sign = $this->sign();
        $public_key = $this->getPublicKey();
        $sign = base64_decode($sign);
        $check = openssl_verify($this->sign_data, $sign, $public_key);
        openssl_free_key($public_key);
        return $check ? 'success' : 'fail';
    }

    /**
     * rsa加密数据
     * @author Adam
     * @date 2019/4/12 16:10
     */
    public function encrypt()
    {
        $public_key = $this->getPublicKey();
        $sign_data_arr = str_split($this->sign_data, 245);
        foreach ($sign_data_arr as $item) {
            $encrypt_item = null;
            $result = openssl_public_encrypt($item, $encrypt_item, $public_key);
            $encrypt_data[] = $encrypt_item;
//            var_dump($result);
        }
        openssl_free_key($public_key);
        return implode('', $encrypt_data);
    }

    /**
     * rsa解密
     * @return string
     * @author Adam
     * @date 2019/4/12 16:33
     */
    public function decrypt()
    {
        $raw = 1;
        return sha1('123213123123asdfsafsdlajfklsadkjf;lsdajf;lksjad;lfjsadjf;sdakfj;sadlkjfsad;lkjflskdajf;lasdjf;lsadjf;alsj',true);
        $encrypt_data = $this->encrypt();

        $private_key = $this->getPrivateKey();
        $maxlength = 256;
        $count = strlen($encrypt_data) / $maxlength;
        $decrypt_str = '';
        for ($i = 0; $i < $count;$i++){
            $encrypt_item = substr($encrypt_data,$i*$maxlength,$maxlength);
           $result = openssl_private_decrypt(($encrypt_item),$decrypt_item,$private_key);
           var_dump($result);
            $decrypt_str .= $decrypt_item;
        }
        openssl_free_key($private_key);
//        var_dump(strlen($this->sign_data))
        return $decrypt_str==$this->sign_data?'sucess':'fail';
    }
}